class ActionsSet{
    constructor(interalArg, actionsArrayArg){
        this.actions = actionsArrayArg;
        this.interval = interalArg;
        this.next = null;
    }
}

export default class record {
    constructor(drawingArg) {
        let  self = this;
        this.drawing = drawingArg;

        this.buffer = new Array(); //array of Point objects
        this.timeInterval = 100; //10 miliseconds
        this.currTime = 0;
        this.started = false;
        this.intervalId = null;
        this.currTimeSlot = 0;
        this.actionsSet = null; //of type ActionSet
        this.currActionSet = null;
        this.recStartTime = null;
        this.pauseInfo = null;
        this.points = [];
        this.totalPauseTime = 0;
        this.pauseStartTime = 0;
    }
    
    start() {
        this.currTime = 0;
        this.currTimeSlot = -1;
        this.actionsSet = null;
        this.pauseInfo = null;

        this.recStartTime = (new Date()).getTime();
        this.intervalId = setInterval(()=>{
            this.onInterval();
        }, this.timeInterval);
        this.started = true;
    }
    stop() {
        if (this.intervalId != null) {
            clearInterval(this.intervalId);
            this.intervalId = null;
        }
        this.started = false;
    }
    pause() {
        this.pauseStartTime = (new Date()).getTime();
        clearInterval(this.intervalId);
    }
    resumeRecording() {
        this.totalPauseTime += (new Date()).getTime() - this.pauseStartTime;
        this.pauseStartTime = 0;
        this.intervalId = setInterval(()=>{
            this.onInterval();
        }, this.timeInterval);
    }
    onInterval(){
        // console.log(this.buffer);
        if (this.buffer.length > 0) {
            const timeSlot = (new Date()).getTime() - this.recStartTime - this.totalPauseTime;

            if (this.currActionSet == null) {
                this.currActionSet = new ActionsSet(timeSlot, this.buffer);
                this.actionsSet = this.currActionSet;
            } else {
                const tmpActionSet = this.currActionSet;
                this.currActionSet = new ActionsSet(timeSlot, this.buffer);
                tmpActionSet.next = this.currActionSet;
            }

            this.buffer = new Array();
        }
        this.currTime += this.timeInterval;
    }
    addAction(actionArg) {
        if (!this.started)
            return;
        this.buffer.push(actionArg);
    }
    processDotted(ps){
        let points = [];
        let len = ps.length;

        let temp = Math.floor(len/2);
        
        for(let i=0;i<len;i++){
     
            if(i%2 == 0){
                
                points.push(ps[i]);

            }

            
        }
        return  points;
    }
    playRecording() {
        if (this.actionsSet == null) {
            // if (typeof onPlayEnd != 'undefined' && onPlayEnd != null)
            //     onPlayEnd();
            return;
        }
        // console.log(this.points);
        // this.points = this.processDotted(this.points);
        // console.log(this.points);
        // this.drawActions(this.points);
        // console.log(this.actionsSet)
        // console.log(this.actionsSet.interval);
        // this.scheduleDraw(this.actionsSet, this.actionsSet.interval, callbackFunctionArg, onPlayEnd, onPause, true, interruptActionStatus);
        this.scheduleDraw(this.actionsSet, this.actionsSet.interval, true);
    }
    scheduleDraw(actionSetArg,
        interval,
       
        isFirst
        ) {
        setTimeout(() => {
            //let status = "";
            // if (interruptActionStatus != null) {
            //     status = interruptActionStatus();
            //     if (status == 'stop') {
            //         this.pauseInfo = null;
            //         onPlayEnd();
            //         return;
            //     }
            // }

            // if (status == "pause") {
            //     this.pauseInfo = {
            //         "actionset": actionSetArg,
            //         "callbackFunc": callbackFunctionArg,
            //         "onPlaybackEnd": onPlayEnd,
            //         "onPause": onPause,
            //         "isFirst": isFirst,
            //         "interruptActionsStatus": interruptActionStatus
            //     };

            //     if (onPause)
            //         onPause();
            //     return;
            // }

            let intervalDiff = -1;
            let isLast = true;
            if (actionSetArg.next != null) {
                isLast = false;
                intervalDiff = actionSetArg.next.interval - actionSetArg.interval;
            }
            // console.log(intervalDiff+"++++++")
            if (intervalDiff >= 0)
                this.scheduleDraw(actionSetArg.next, intervalDiff, false);

            this.drawActions(actionSetArg.actions, isFirst, isLast);
        }, interval);

    }
    resume() {
        if (!this.pauseInfo)
            return;

        this.scheduleDraw(this.pauseInfo.actionset, 0,
            this.pauseInfo.callbackFunc,
            this.pauseInfo.onPlaybackEnd,
            this.pauseInfo.onPause,
            this.pauseInfo.isFirst,
            this.pauseInfo.interruptActionsStatus);

        this.pauseInfo = null;
    }
   
    drawActions(actionArray){
        for (let i = 0; i < actionArray.length; i++){
      
                this.drawing.drawAction(actionArray[i],false);

        }
        this.drawing.ctx.fill();
        this.drawing.ctx.draw(true);
		// if (isLast)
		// {
            
		// 	onPlayEnd();
		// }
    }
}
